<?php
header("Access-Control-Allow-Origin: *");
class TrademarksAction extends CommonAction {

    //初始化
    protected function _initialize(){
        $this->redis_cache_connect();
    }

    public function detail(){
        $trademarkid = htmlspecialchars(trim($this->_get('tid')));
        //查询商标信息
        $trademark = M('trademarks m')->where(array( "id"=>$trademarkid, 'is_deleted' => 0, 'status' => 1, 'image_status' => 1))->field('m.*')->find();
        if( empty($trademark) ){
            $this->error_notice("商标信息不存在，具体信息可咨询商标顾问！");
        }
        if( $trademark ){
            //查询同名多类商标
            if( trim($trademark["trademark_name"]) != '图形' && trim($trademark["trademark_name"]) != "" ){
                $trademarkSameNames = M('trademarks m')
                    ->join("trademarks_category c on m.trademark_cate = c.id")
                    ->where(array("m.trademark_name"=>$trademark["trademark_name"], 'm.status' => ["in",[0,1]], "m.is_deleted" => 0))
                    ->field('m.trademark_name, m.trademark_cate, c.cate_sn')
                    ->select();
                if( $trademarkSameNames  ){
                    $trademark['trademark_same_names'] = $trademarkSameNames;
                }
            }
            //查询商标分类
            $cateInfo = M('trademarks_category')->where(array('id'=>$trademark['trademark_cate']))->find();
            $trademark['cate_info'] = $cateInfo;
            //基础查询条件
            $trademark_sn = $trademark['trademark_sn'];
            $trademark_cate = $trademark['trademark_cate'];
            //注册商标基本信息
            $filter = ['trademark_sn' => $trademark_sn, "international_cate" => $trademark_cate];
            $options = ['sort'=>array('_id' => -1), 'limit' =>1];
            $queryTbi = new MongoDB\Driver\Query($filter, $options);
            $tbiObj = $this->mongo->executeQuery('dunmama.trademark_base_info', $queryTbi)->toArray();
            if( $tbiObj != null && count($tbiObj) > 0 ){
                $trademark_base_info = json_decode(json_encode($tbiObj[0]), true);
                $trademark_base_info['exclusive_end_time'] = !empty($trademark_base_info['exclusive_end_time']) ? msubstr($trademark_base_info['exclusive_end_time'], 0, 10) : "";
                $trademark_base_info['exclusive_start_time'] = !empty($trademark_base_info['exclusive_start_time']) ? msubstr($trademark_base_info['exclusive_start_time'], 0, 10) : "";
                $trademark_base_info['registered_notice_time'] = !empty($trademark_base_info['registered_notice_time']) ? msubstr($trademark_base_info['registered_notice_time'], 0, 10) : "";
                $trademark_base_info['first_trial_notice_time'] = !empty($trademark_base_info['first_trial_notice_time']) ? msubstr($trademark_base_info['first_trial_notice_time'], 0, 10) : "";
                $trademark['trademark_base_info'] = $trademark_base_info;
            }
            //商标注册人信息
            $query = new MongoDB\Driver\Query($filter, $options);
            $triObj = $this->mongo->executeQuery('dunmama.trademark_register_info', $query)->toArray();
            if( $triObj != null && count($triObj) > 0 ){
                $trademark_register_info = json_decode(json_encode($triObj[0]), true);
                $trademark['trademark_register_info'] = $trademark_register_info;
                //查询商标注册人的申请量
                if( $trademark['trademark_register_info']['register_chinese_name'] ){
                    $countFilter = ['register_chinese_name' => $trademark['trademark_register_info']['register_chinese_name']];
                    $countQuery = new MongoDB\Driver\Query($countFilter, ['sort'=>array('_id' => -1)]);
                    $countObj = $this->mongo->executeQuery('dunmama.trademark_register_info', $countQuery)->toArray();
                    $trademark['trademark_register_info']['trademark_apply_count'] = count($countObj);
                }
            }
            //注册商标商品服务信息
            if( $trademark['trademark_group_sn'] ){
                $trademark['trademark_group_sn'] = json_decode($trademark['trademark_group_sn'],true);
            }
            if( $trademark['trademark_goods_name'] ){
                $goods_chinese_name = array();
                $goods_chinese_name_json = json_decode($trademark['trademark_goods_name'],true);
                if( !empty($goods_chinese_name_json) ){
                    foreach ($goods_chinese_name_json as $key => $value){
                        for ( $i = 0; $i < count($value); $i++ ){
                            if( !in_array($value[$i],$goods_chinese_name) ){
                                array_push($goods_chinese_name,$value[$i]);
                            }
                        }
                    }
                }
                $trademark['trademark_goods_name'] = $goods_chinese_name;
            }
            //注册商标共有人信息
            $queryCoowner = new MongoDB\Driver\Query(['trademark_sn' => $trademark_sn], ['sort'=>array('_id' => -1)]);
            $coownerObj = $this->mongo->executeQuery('dunmama.trademark_coowner_info', $queryCoowner)->toArray();
            if( $coownerObj != null && count($coownerObj) > 0 ){
                $trademark_coowner_info = json_decode(json_encode($coownerObj), true);
                $trademark['trademark_coowner_info'] = $trademark_coowner_info;
            }
        }
        $this->assign('trademark',$trademark);
        $this->display();
    }

    public function details(){
        $id = $this->_get('tid','trim');
        $cacheKey = "nonaodashi_mobile_".$id;
        $jsonData = $this->redis_cache->get($cacheKey);
        $trademark = json_decode($jsonData,true);
        if( empty($trademark) ){
            $ip = empty($_SERVER["REMOTE_ADDR"]) ? "47.102.187.210" : $_SERVER["REMOTE_ADDR"];
            $url = 'https://biaobiaoku.com/api/get-info-by-id?id='.$id."&ip=".$ip;
            $res = file_get_contents($url);
            $response = json_decode($res,true,JSON_UNESCAPED_UNICODE);
            $code = $response['code'];
            if( $code == 0 ){
                $trademark = $response['data'];
                $this->redis_cache->setex($cacheKey, 3600 * 2, json_encode($trademark));
            }
        }
        if( !empty($trademark) ){
            /**
             * 商标价格
             */
            //计算最新价格
            $price = $trademark['price'];
            $newprice = intval($price * (100 + $this->buyFasterRate) / 100);
            if( $newprice < ($price + $this->buyFasterLow) ){
                $trademark['price'] = $price + $this->buyFasterLow;
            }else if( $newprice > ($price + $this->buyFasterHigh) ){
                $trademark['price'] = $price + $this->buyFasterHigh;
            }else{
                $trademark['price'] = $newprice;
            }
            //处理商标图片
            //$trademark['thumb_image'] = "https://color.picer.cdn.biaobiaoku.com/".$trademark['brandId']."-".$trademark['picHash']."-250.png";
            //$trademark['trademark_image'] = "https://brand.picer.cdn.biaobiaoku.com/".$trademark['brandId']."-".$trademark['picHash'].".jpg";
            $trademark['thumb_image'] = $this->getImageUrl($trademark,2);
            $trademark['trademark_image'] = $this->getImageUrl($trademark,0);
            //处理同名多类
            if( !empty($trademark['brother']) && strtolower($trademark['brother']) != 'null' ){
                $brolist = explode(";",$trademark['brother']);
                $brolist = array_filter($brolist, function($value) {
                    return $value !== '';
                });
                $trademark['brotherCount'] = count( array_filter($brolist) );
            }else{
                $trademark['brother'] = "";
            }
            $trademark['lei_name'] = $this->getCategory($trademark['lei']);
            $this->assign('trademark',$trademark);
        }else{
            $this->error_notice("商标信息不存在");
        }
        $this->assign('sm',3);
        $this->display();
    }

    public function getDetail(){
        $id = $this->_get('tid','trim');
        $cacheKey = "nonaodashi_mobile_".$id;
        $jsonData = $this->redis_cache->get($cacheKey);
        $trademark = json_decode($jsonData,true);
        if( empty($trademark) ){
            $ip = empty($_SERVER["REMOTE_ADDR"]) ? "47.102.187.210" : $_SERVER["REMOTE_ADDR"];
            $url = 'https://biaobiaoku.com/api/get-info-by-id?id='.$id."&ip=".$ip;
            $res = file_get_contents($url);
            $response = json_decode($res,true,JSON_UNESCAPED_UNICODE);
            $code = $response['code'];
            if( $code == 0 ){
                $trademark = $response['data'];
                $this->redis_cache->setex($cacheKey, 3600 * 2, json_encode($trademark));
            }
        }
        if( empty($trademark) ){
            $where = array();
            $where['relateid'] = $id;
            $where['is_deleted'] = 0;
            $info = M("trademarks_best")->where($where)->find();
            if( !empty($info) ){
                $data = array();
                $data['is_deleted'] = 1;
                $data['remark'] = "获取商标仓库商标信息失败！反馈内容为空！";
                $data['update_at'] = date("Y-m-d H:i:s");
                $data['deleted_at'] = date("Y-m-d H:i:s");
                M("trademarks_best")->where($where)->save($data);
            }
            echo json_encode(['code' => -1, 'msg' => "商标信息不存在", 'data' => []],JSON_UNESCAPED_UNICODE);
            exit;
        }
        /**
         * 商标价格
         */
        $price = $trademark['price'];
        $newprice = intval($price * (100 + $this->buyFasterRate) / 100);
        if( $newprice < ($price + $this->buyFasterLow) ){
            $trademark['price'] = $price + $this->buyFasterLow;
        }else if( $newprice > ($price + $this->buyFasterHigh) ){
            $trademark['price'] = $price + $this->buyFasterHigh;
        }else{
            $trademark['price'] = $newprice;
        }
        //处理商标图片
        //$trademark['thumb_image'] = "https://color.picer.cdn.biaobiaoku.com/".$trademark['brandId']."-".$trademark['picHash']."-250.png";
        //$trademark['trademark_image'] = "https://brand.picer.cdn.biaobiaoku.com/".$trademark['brandId']."-".$trademark['picHash'].".jpg";
        $trademark['thumb_image'] = $this->getImageUrl($trademark,2);
        $trademark['trademark_image'] = $this->getImageUrl($trademark,0);
        //处理同名多类
        if( !empty($trademark['brother']) && strtolower($trademark['brother']) != 'null' ){
            $brolist = explode(";",$trademark['brother']);
            $brolist = array_filter($brolist, function($value) {
                return $value !== '';
            });
            $trademark['brotherCount'] = count( array_filter($brolist) );
        }else{
            $trademark['brother'] = "";
            $trademark['brotherCount'] = 1;
        }
        $trademark['lei_name'] = $this->getCategory($trademark['lei']);
        if( !empty($trademark['sqName']) ){
            $trademark['sqName'] = hideusername($trademark['sqName']);
        }
        $concat = array();
        $concat['user_name'] = "商标顾问 小闹";
        $concat['mobile'] = "13838111503";
        $concat['qrcode_url'] = 'https://www.naonaodashi.com/static/img/guwen.jpg?v=20240707';
        $trademark['concat'] = $concat;
        echo json_encode(['code' => 0, 'msg' => "", 'data' => $trademark],JSON_UNESCAPED_UNICODE);
        exit;
    }

    public function viewImage(){
        $imageSrc = htmlspecialchars(trim($this->_get('imageSrc')));
        $this->assign('imageSrc',$imageSrc);
        $this->display();
    }

}
?>